define([ "app", "jquery", "bootstrapJs","md5", "ecb","angularRadial","radial","highcharts","highcharts-ng","artDialog","iframeTools",
         "directives/DatetimePicker",
         "services/UserService",
         "resources/OrgResource",
		 "resources/UserResource",
		 "resources/UserSettingResource",
		 "services/MessageBox",
		 "directives/common/OrgUserPickerConference",
		 "directives/common/OrgTreeView",
		 "services/TipsService",
		 "services/BaseService"
		],
		function(app) {

	var deps = [ "$scope", "$state", "$window",  "$location",
			"$stateParams", "MenuService", "UserService","OrgResource","$modalInstance","UserSettingResource", "UserResource","MessageBox","TipsService","BaseService"];

	function controller($scope, $state, $window, $location, $stateParams, MenuService, UserService,OrgResource,$modalInstance,UserSettingResource, UserResource,MessageBox,TipsService,BaseService) {
			

		$scope.user = UserService.getCurrentUser();
		var userId =$scope.user.userId;
		
		$scope.updatePassword = function() {
			
			var oldpassword =encrypt($scope.oldpassword, $scope.user.userName);
			var newpassword =encrypt($scope.newpassword, $scope.user.userName);
			
			UserSettingResource.updatePassword(userId,oldpassword,newpassword).success(function(data){
				
				var code =data.resCode;
				
				if(code=="0000"){
					art.dialog.alert("修改密码成功!");
					$modalInstance.close(1);
				}
				else{
					art.dialog.alert("修改密码失败!");
				}
            });
		};
		
		$scope.close = function($scope) {
			$modalInstance.close(2);
		};
    };
    
    /**
	 * 密码 encrypt 加密
	 */
	function encrypt(content,srcKey){
	      var keyLen = 8;
		  var srcKeyLen = srcKey.length;
		
		 if(srcKeyLen > keyLen){
			 srcKey = srcKey.substr(0,keyLen);
			
		 }else{
			 var paddLen = keyLen -  srcKeyLen;
			 for(var i=0;i<paddLen;i++){
				 srcKey += "0";
			 }
		 }
		
		 var key = CryptoJS.enc.Utf8.parse(srcKey);		
		 var encryptStr =  CryptoJS.DES.encrypt(content, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
		return encryptStr.toString();
	}
	
    controller.$inject = deps;
    return app.lazy.controller("UpdatePasswordController", controller);
});
